package com.zhongketech.sunflower.activity;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.drawable.Drawable;
import android.media.ThumbnailUtils;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.PowerManager;
import android.provider.MediaStore;
import android.provider.MediaStore.Video.Thumbnails;
import android.support.v4.view.ViewPager;
import android.text.ClipboardManager;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zhongketech.sunflower.Constant;
import com.zhongketech.sunflower.MyToast;
import com.zhongketech.sunflower.R;
import com.zhongketech.sunflower.adapter.ExpressionAdapter;
import com.zhongketech.sunflower.adapter.ExpressionPagerAdapter;
import com.zhongketech.sunflower.adapter.MessageAdapter;
import com.zhongketech.sunflower.adapter.VoicePlayClickListener;
import com.zhongketech.sunflower.control.PhoneManager;
import com.zhongketech.sunflower.sv.SvManager;
import com.zhongketech.sunflower.transport.LogUtil;
import com.zhongketech.sunflower.transport.model.MySupervision;
import com.zhongketech.sunflower.transport.model.Supervision;
import com.zhongketech.sunflower.transport.model.Times;
import com.zhongketech.sunflower.utils.CommonUtils;
import com.zhongketech.sunflower.utils.ImageUtil;
import com.zhongketech.sunflower.utils.SmileUtils;




/**
 * 控制页面
 * 
 */
public class SupervisonsActivity extends BaseActivity {

	private static final String TAG = "ControlActivity";
	private int[][] iconArray={{R.drawable.fcs_control_phone_disable,R.drawable.fcs_control_phone_enable},
			{R.drawable.fcs_control_sms_disable,R.drawable.fcs_control_sms_enable},
			{R.drawable.fcs_control_lock_disable,R.drawable.fcs_control_lock_enable},
			{R.drawable.fcs_control_net_disable,R.drawable.fcs_control_net_enable},
			{R.drawable.fcs_control_eyes_disable,R.drawable.fcs_control_eyes_enable},
			{R.drawable.fcs_control_app_disable,R.drawable.fcs_control_app_enable}};


	
//	ImageView fcs_control_phone;
//	ImageView fcs_control_sms;
//	ImageView fcs_control_lock;
//	ImageView fcs_control_net;
//	ImageView fcs_control_eyes;
//	ImageView fcs_control_app;
	
	private ListView enableListView ;
	private ListView disableListView ;
	private Context context;

	private List<AdaterData> enableData = new ArrayList<AdaterData>();
	private List<AdaterData> disabelData = new ArrayList<AdaterData>();
	private MyAdapter enableAapter;
	private MyAdapter disableAapter;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.supervisons_activity);
		context = this;
		initView();
		initData();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		initData();
	}

	private void initData()
	{

		enableData.clear();
		disabelData.clear();
		MySupervision ms = SvManager.getInstance().getMySupervision();
		if(null != ms && null != ms.getSupervisons())
		{
			List<Supervision> list = ms.getSupervisons();
			for(int i = 0;i < list.size();i ++)
			{
				if("one_key".equals(list.get(i).getType()))
				{
					continue;
				}
				AdaterData data = new AdaterData();
				data.title = list.get(i).getTitle();
				data.id = list.get(i).getId();
				data.fcs_control_phone = list.get(i).getCall().getEnable();
				data.fcs_control_sms = list.get(i).getSms().getEnable();
				data.fcs_control_lock = list.get(i).getScreen_lock().getEnable();
				data.fcs_control_net = list.get(i).getNetwork().getEnable();
				data.fcs_control_eyes = list.get(i).getEyes_protect().getEnable();
				data.fcs_control_app = true;	
				if(Constant.APP_ENABLE_ALL.equals(list.get(i).getApplication().getEnable()))
				{
					data.fcs_control_app = false;	
				}
				
				data.enable = false;
				Supervision activateSv = SvManager.getInstance().getActivateSv();
				if(null != activateSv && data.id.equals(activateSv.getId()))
				{
					data.enable = true;
				}

				if(list.get(i).getEnable())
				{
					enableData.add(data);
				}
				else
				{
					disabelData.add(data);
				}
			}
		}

//		MyToast.getInstance().show("enable:"+enableData.size()+"  disabel:"+disabelData.size());
		enableAapter.notifyDataSetChanged();
		disableAapter.notifyDataSetChanged();
	}

	private void initView()
	{
		enableListView = (ListView) findViewById(R.id.enableListView);
		enableAapter = new MyAdapter(enableData);
		enableListView.setAdapter(enableAapter);
		enableListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
		        Intent intent = new Intent(context.getApplicationContext(),TimeBucketInfoActivity.class);  
                // 在Intent中传递数据  
                intent.putExtra("id", enableData.get(position).id);  
                // 启动Intent  
                startActivity(intent); 
			}
		});

		disableListView = (ListView) findViewById(R.id.disableListView);
		disableAapter = new MyAdapter(disabelData);
		disableListView.setAdapter(disableAapter);
		disableListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
		        Intent intent = new Intent(context.getApplicationContext(),TimeBucketInfoActivity.class);  
                // 在Intent中传递数据  
                intent.putExtra("id", disabelData.get(position).id);  
                // 启动Intent  
                startActivity(intent); 
			}
		});
	}


	public final class ViewHolder {
		ImageView enable;
		TextView title;
		ImageView fcs_control_phone;
		ImageView fcs_control_sms;
		ImageView fcs_control_lock;
		ImageView fcs_control_net;
		ImageView fcs_control_eyes;
		ImageView fcs_control_app;
	}



	public final class AdaterData{
		String title;
		String id;
		boolean enable;
		boolean fcs_control_phone;
		boolean fcs_control_sms;
		boolean fcs_control_lock;
		boolean fcs_control_net;
		boolean fcs_control_eyes;
		boolean fcs_control_app;
	}

	public class MyAdapter extends BaseAdapter
	{
		private List<AdaterData> myData;

		MyAdapter(List<AdaterData> data)
		{
			myData = data;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return myData.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub

			ViewHolder holder = null;
			if(null == convertView)
			{
				holder = new ViewHolder(); 
				convertView =  LayoutInflater.from(context).inflate(R.layout.supervison_item, null);
				holder.enable = (ImageView) convertView.findViewById(R.id.enable);
				holder.title = (TextView) convertView.findViewById(R.id.title);
				holder.fcs_control_phone = (ImageView) convertView.findViewById(R.id.fcs_control_phone);
				holder.fcs_control_sms = (ImageView) convertView.findViewById(R.id.fcs_control_sms);
				holder.fcs_control_lock = (ImageView) convertView.findViewById(R.id.fcs_control_lock);
				holder.fcs_control_net = (ImageView) convertView.findViewById(R.id.fcs_control_net);
				holder.fcs_control_eyes = (ImageView) convertView.findViewById(R.id.fcs_control_eyes);
				holder.fcs_control_app = (ImageView) convertView.findViewById(R.id.fcs_control_app);
				convertView.setTag(holder);
			}
			else
			{
				holder = (ViewHolder)convertView.getTag();
			}

			if(myData.get(position).enable)
			{
				holder.enable.setVisibility(View.VISIBLE);
			}
			else
			{
				holder.enable.setVisibility(View.GONE);
			}
			
			holder.title.setText(myData.get(position).title);
			holder.fcs_control_phone.setImageResource(iconArray[0][(myData.get(position).fcs_control_phone) ? 1 : 0]);
			holder.fcs_control_sms.setImageResource(iconArray[1][(myData.get(position).fcs_control_sms) ? 1 : 0]);
			holder.fcs_control_lock.setImageResource(iconArray[2][(myData.get(position).fcs_control_lock) ? 1 : 0]);
			holder.fcs_control_net.setImageResource(iconArray[3][(myData.get(position).fcs_control_net) ? 1 : 0]);
			holder.fcs_control_eyes.setImageResource(iconArray[4][(myData.get(position).fcs_control_eyes) ? 1 : 0]);
			holder.fcs_control_app.setImageResource(iconArray[5][(myData.get(position).fcs_control_app) ? 1 : 0]);
			
			return convertView;
		}

	}

}
